package com.cms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrBankAccount;
import com.cms.service.BankInfoService;
import com.cms.service.ReportService;
import com.google.gson.Gson;

@Controller
public class BankAccountController extends BaseController {

	@Autowired
	private BankInfoService bankInfoService;
	
	@Autowired  
	private ReportService reportService;
	
	@RequestMapping("/bankAccount/pageBankAccountList.do")
	public String pageBankInfoList(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		try
		{
		map.put("bankInfoList", this.reportService.pagingReportResult("pagingBankAccountList", this.buildQueryMap(req)));
		return "bank/account/bankAccountList.html";
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@RequestMapping("/bankAccount.do")
	public String main(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		map.put("bankList", this.bankInfoService.getBankInfoList());
		return "bank/account/bankAccountMain.html";
	}
	
	
	@RequestMapping("/bankAccount/toRegBankAccount.do")
	public String toRegBankAccount(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		TrBankAccount account = new TrBankAccount();
		map.put("action", "bankAccount/addBankAccount.do");
		map.put("account",account);
		map.put("bankList", this.bankInfoService.getBankInfoList());
		return "bank/account/bankAccountInfo.html";
	}
	
	
	@RequestMapping("/bankAccount/toEditBankAccount.do")
	public String toEditBankAccount(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		TrBankAccount account = new TrBankAccount();
		account.setId(StringUtils.str2Int(req.getParameter("accoutId")));
		map.put("action", "bankAccount/updateBankAccount.do");
		map.put("account",this.bankInfoService.getBankAccountInfo(account));
		map.put("bankList", this.bankInfoService.getBankInfoList());
		return "bank/account/bankAccountInfo.html";
	}
	
	
	@RequestMapping("/bankAccount/addBankAccount.do")
	public void addBankAccount(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		TrBankAccount account = new TrBankAccount();
		TrAdminUser tu = this.getLoginUser(req);
		account.setCreateUser(tu.getId());
		account.setModifyUser(tu.getId());
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			this.export(req, "account", account);
			this.bankInfoService.saveBankAccount(account);
			param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			param.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(param));
	}
	
	@RequestMapping("/bankAccount/updateBankAccount.do")
	public void updateBankAccount(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		TrBankAccount account = new TrBankAccount();
		TrAdminUser tu = this.getLoginUser(req);
		account.setCreateUser(tu.getId());
		account.setModifyUser(tu.getId());
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			this.export(req, "account", account);
			this.bankInfoService.updateBankAccount(account);
			param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			param.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(param));
	} 
}
